package book.book.dao;

import book.book.bean.Borrowinfo;
import book.book.util.Base;

import java.util.List;

public class BorrowinfoDaoImpl extends Base implements BorrowinfoDao {
    @Override
    public List<Borrowinfo> selectBorrwInfoByBookId(String id) {
        String sql = "select * from tb_borrowinfo where bookid =? and isreturn = 0";
        return getAll(Borrowinfo.class, sql, id);
    }

    @Override
    public int insert(Borrowinfo borrowinfo) {
        String sql = "insert into tb_borrowinfo values(?,?,?,?,?,?,?)";
        return update(sql, borrowinfo.getBorrowid(), borrowinfo.getBookid(),
                borrowinfo.getBorrower(), borrowinfo.getPhone(),
                borrowinfo.getBorrowtime(), null, 0);

    }

    @Override
    public List<Borrowinfo> selectBorrwInfoByUser(String borrower) {
        String sql = "select * from tb_borrowinfo where borrower =? and isreturn = 0";
        return getAll(Borrowinfo.class, sql, borrower);
    }

    @Override
    public int updateBookCount(String bookId) {
        String sql = "update tb_books set remain = remain - 1 where bookid =?";
        return update(sql, bookId);
    }

    @Override
    public List<Borrowinfo> selectBinfoByUser(String borrower) {
        String sql = "select * from tb_borrowinfo where borrower =?";
        return getAll(Borrowinfo.class, sql, borrower);
    }

    @Override
    public int updateBookCountBack(String bookId ,String borrower) {
        String sql = "update tb_borrowinfo set isreturn = 1,returntime = NOW() where bookid =? and borrower =?";
        String sql1 = "update tb_books set remain = remain + 1 where bookid =? ";
        update(sql, bookId, borrower);
        return update(sql1, bookId);
    }



}




